package com.firstsight.micro.homedo.service.impl;

import com.firstsight.micro.homedo.service.IThreadsService;
import jodd.time.TimeUtil;
import jodd.util.concurrent.ThreadFactoryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

@Service
public class ThreadsServiceImpl implements IThreadsService {
    @Override
    public List<String> executerServiceTest() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("pool-线程池-%d").get();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
                10, 0L,
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(10),
                threadFactory, new ThreadPoolExecutor.AbortPolicy());
        List<String> res = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            threadPoolExecutor.execute(()->{
                try {
                    Thread.sleep(1000);
                    res.add(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        threadPoolExecutor.shutdown();
        try {
            threadPoolExecutor.awaitTermination(20L,TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return res;
    }
}
